package com.automata.utils 
{
	import com.automata.status.HangulCode;
	/**
	 * 한글 조합, 호환 한글 변환, 일반 한글 변환등의 일을 수행
	 * @author kssuman
	 */
	public class HangulUtil
	{
		private static var cho2JongTable:Object;
		private static var jongSeongsTable:Object;
		private static var jcTable:Object;	// 종성 --> 종성과 초성으로 분리해줌
		private static var jungSeongsTable:Object;
		private static var cho2CompatiChoTable:Object;
		
		public static function init():void {
			cho2JongTable = new Object();
			cho2JongTable[HangulCode.CHOSEONG_KIYEOK] = HangulCode.JONGSEONG_KIYEOK;
			cho2JongTable[HangulCode.CHOSEONG_SSANGKIYEOK] = HangulCode.JONGSEONG_SSANGKIYEOK;
			cho2JongTable[HangulCode.CHOSEONG_NIEUN] = HangulCode.JONGSEONG_NIEUN;
			cho2JongTable[HangulCode.CHOSEONG_TIKEUT] = HangulCode.JONGSEONG_TIKEUT;
			cho2JongTable[HangulCode.CHOSEONG_RIEUL] = HangulCode.JONGSEONG_RIEUL;
			cho2JongTable[HangulCode.CHOSEONG_MIEUM] = HangulCode.JONGSEONG_MIEUM;
			cho2JongTable[HangulCode.CHOSEONG_PIEUP] = HangulCode.JONGSEONG_PIEUP;
			cho2JongTable[HangulCode.CHOSEONG_SIOS] = HangulCode.JONGSEONG_SIOS;
			cho2JongTable[HangulCode.CHOSEONG_SSANGSIOS] = HangulCode.JONGSEONG_SSANGSIOS;
			cho2JongTable[HangulCode.CHOSEONG_IEUNG] = HangulCode.JONGSEONG_IEUNG;
			cho2JongTable[HangulCode.CHOSEONG_CIEUC] = HangulCode.JONGSEONG_CIEUC;
			cho2JongTable[HangulCode.CHOSEONG_CHIEUCH] = HangulCode.JONGSEONG_CHIEUCH;
			cho2JongTable[HangulCode.CHOSEONG_KHIEUKH] = HangulCode.JONGSEONG_KHIEUKH;
			cho2JongTable[HangulCode.CHOSEONG_THIEUTH] = HangulCode.JONGSEONG_THIEUTH;
			cho2JongTable[HangulCode.CHOSEONG_PHIEUPH] = HangulCode.JONGSEONG_PHIEUPH;
			cho2JongTable[HangulCode.CHOSEONG_HIEUH] = HangulCode.JONGSEONG_HIEUH;
			
			jongSeongsTable = new Object();
			jongSeongsTable[HangulCode.JONGSEONG_KIYEOK + "_" + HangulCode.JONGSEONG_SIOS] = HangulCode.JONGSEONG_KIYEOK_SIOS;
			jongSeongsTable[HangulCode.JONGSEONG_NIEUN + "_" + HangulCode.JONGSEONG_CIEUC] = HangulCode.JONGSEONG_NIEUN_CIEUC;
			jongSeongsTable[HangulCode.JONGSEONG_NIEUN + "_" + HangulCode.JONGSEONG_HIEUH] = HangulCode.JONGSEONG_NIEUN_HIEUH;
			jongSeongsTable[HangulCode.JONGSEONG_RIEUL + "_" + HangulCode.JONGSEONG_KIYEOK] = HangulCode.JONGSEONG_RIEUL_KIYEOK;
			jongSeongsTable[HangulCode.JONGSEONG_RIEUL + "_" + HangulCode.JONGSEONG_MIEUM] = HangulCode.JONGSEONG_RIEUL_MIEUM;
			jongSeongsTable[HangulCode.JONGSEONG_RIEUL + "_" + HangulCode.JONGSEONG_PIEUP] = HangulCode.JONGSEONG_RIEUL_PIEUP;
			jongSeongsTable[HangulCode.JONGSEONG_RIEUL + "_" + HangulCode.JONGSEONG_SIOS] = HangulCode.JONGSEONG_RIEUL_SIOS;
			jongSeongsTable[HangulCode.JONGSEONG_RIEUL + "_" + HangulCode.JONGSEONG_THIEUTH] = HangulCode.JONGSEONG_RIEUL_THIEUTH;
			jongSeongsTable[HangulCode.JONGSEONG_RIEUL + "_" + HangulCode.JONGSEONG_PHIEUPH] = HangulCode.JONGSEONG_RIEUL_PHIEUPH;
			jongSeongsTable[HangulCode.JONGSEONG_RIEUL + "_" + HangulCode.JONGSEONG_HIEUH] = HangulCode.JONGSEONG_RIEUL_HIEUH;
			jongSeongsTable[HangulCode.JONGSEONG_PIEUP + "_" + HangulCode.JONGSEONG_SIOS] = HangulCode.JONGSEONG_PIEUP_SIOS;
			
			jcTable = new Object();
			jcTable[HangulCode.JONGSEONG_KIYEOK] = [HangulCode.JONGSEONG_NONE, HangulCode.CHOSEONG_KIYEOK]; 
			jcTable[HangulCode.JONGSEONG_SSANGKIYEOK] = [HangulCode.JONGSEONG_NONE, HangulCode.CHOSEONG_SSANGKIYEOK]; 
			jcTable[HangulCode.JONGSEONG_KIYEOK_SIOS] = [HangulCode.JONGSEONG_KIYEOK, HangulCode.CHOSEONG_SIOS];
			jcTable[HangulCode.JONGSEONG_NIEUN] = [HangulCode.JONGSEONG_NONE, HangulCode.CHOSEONG_NIEUN];
			jcTable[HangulCode.JONGSEONG_NIEUN_CIEUC] = [HangulCode.JONGSEONG_NIEUN, HangulCode.CHOSEONG_CIEUC];
			jcTable[HangulCode.JONGSEONG_NIEUN_HIEUH] = [HangulCode.JONGSEONG_NIEUN, HangulCode.CHOSEONG_HIEUH];
			jcTable[HangulCode.JONGSEONG_TIKEUT] = [HangulCode.JONGSEONG_NONE, HangulCode.CHOSEONG_TIKEUT];
			jcTable[HangulCode.JONGSEONG_RIEUL] = [HangulCode.JONGSEONG_NONE, HangulCode.CHOSEONG_RIEUL];
			jcTable[HangulCode.JONGSEONG_RIEUL_KIYEOK] = [HangulCode.JONGSEONG_RIEUL, HangulCode.CHOSEONG_KIYEOK];
			jcTable[HangulCode.JONGSEONG_RIEUL_MIEUM] = [HangulCode.JONGSEONG_RIEUL, HangulCode.CHOSEONG_MIEUM];
			jcTable[HangulCode.JONGSEONG_RIEUL_PIEUP] = [HangulCode.JONGSEONG_RIEUL, HangulCode.CHOSEONG_PIEUP]; 
			jcTable[HangulCode.JONGSEONG_RIEUL_SIOS] = [HangulCode.JONGSEONG_RIEUL, HangulCode.CHOSEONG_SIOS]; 
			jcTable[HangulCode.JONGSEONG_RIEUL_THIEUTH] = [HangulCode.JONGSEONG_RIEUL, HangulCode.CHOSEONG_THIEUTH]; 
			jcTable[HangulCode.JONGSEONG_RIEUL_PHIEUPH] = [HangulCode.JONGSEONG_RIEUL, HangulCode.CHOSEONG_PHIEUPH]; 
			jcTable[HangulCode.JONGSEONG_RIEUL_HIEUH] = [HangulCode.JONGSEONG_RIEUL, HangulCode.CHOSEONG_HIEUH]; 
			jcTable[HangulCode.JONGSEONG_MIEUM] = [HangulCode.JONGSEONG_NONE, HangulCode.CHOSEONG_MIEUM]; 
			jcTable[HangulCode.JONGSEONG_PIEUP] = [HangulCode.JONGSEONG_NONE, HangulCode.CHOSEONG_PIEUP]; 
			jcTable[HangulCode.JONGSEONG_PIEUP_SIOS] = [HangulCode.JONGSEONG_PIEUP, HangulCode.CHOSEONG_SIOS];
			jcTable[HangulCode.JONGSEONG_SIOS] = [HangulCode.JONGSEONG_NONE, HangulCode.CHOSEONG_SIOS];
			jcTable[HangulCode.JONGSEONG_SSANGSIOS] = [HangulCode.JONGSEONG_NONE, HangulCode.CHOSEONG_SSANGSIOS];
			jcTable[HangulCode.JONGSEONG_IEUNG] = [HangulCode.JONGSEONG_NONE, HangulCode.CHOSEONG_IEUNG];
			jcTable[HangulCode.JONGSEONG_CIEUC] = [HangulCode.JONGSEONG_NONE, HangulCode.CHOSEONG_CIEUC];
			jcTable[HangulCode.JONGSEONG_CHIEUCH] = [HangulCode.JONGSEONG_NONE, HangulCode.CHOSEONG_CHIEUCH];
			jcTable[HangulCode.JONGSEONG_KHIEUKH] = [HangulCode.JONGSEONG_NONE, HangulCode.CHOSEONG_KHIEUKH];
			jcTable[HangulCode.JONGSEONG_THIEUTH] = [HangulCode.JONGSEONG_NONE, HangulCode.CHOSEONG_THIEUTH];
			jcTable[HangulCode.JONGSEONG_PHIEUPH] = [HangulCode.JONGSEONG_NONE, HangulCode.CHOSEONG_PHIEUPH];
			jcTable[HangulCode.JONGSEONG_HIEUH] = [HangulCode.JONGSEONG_NONE, HangulCode.CHOSEONG_HIEUH];
			
			jungSeongsTable = new Object();
			jungSeongsTable[HangulCode.JUNGSEONG_O + "_" + HangulCode.JUNGSEONG_A] = HangulCode.JUNGSEONG_WA;
			jungSeongsTable[HangulCode.JUNGSEONG_O + "_" + HangulCode.JUNGSEONG_AE] = HangulCode.JUNGSEONG_WAE;
			jungSeongsTable[HangulCode.JUNGSEONG_O + "_" + HangulCode.JUNGSEONG_I] = HangulCode.JUNGSEONG_OE;
			jungSeongsTable[HangulCode.JUNGSEONG_U + "_" + HangulCode.JUNGSEONG_EO] = HangulCode.JUNGSEONG_WEO;
			jungSeongsTable[HangulCode.JUNGSEONG_U + "_" + HangulCode.JUNGSEONG_E] = HangulCode.JUNGSEONG_WE;
			jungSeongsTable[HangulCode.JUNGSEONG_U + "_" + HangulCode.JUNGSEONG_I] = HangulCode.JUNGSEONG_WI;
			jungSeongsTable[HangulCode.JUNGSEONG_EU + "_" + HangulCode.JUNGSEONG_I] = HangulCode.JUNGSEONG_YI;
			
			cho2CompatiChoTable = new Object();
			cho2CompatiChoTable[HangulCode.CHOSEONG_KIYEOK] = HangulCode.COMPATI_CHOSEONG_KIYEOK;
			cho2CompatiChoTable[HangulCode.CHOSEONG_SSANGKIYEOK] = HangulCode.COMPATI_CHOSEONG_SSANGKIYEOK;
			cho2CompatiChoTable[HangulCode.CHOSEONG_NIEUN] = HangulCode.COMPATI_CHOSEONG_NIEUN;
			cho2CompatiChoTable[HangulCode.CHOSEONG_TIKEUT] = HangulCode.COMPATI_CHOSEONG_TIKEUT;
			cho2CompatiChoTable[HangulCode.CHOSEONG_SSANGTIKEUT] = HangulCode.COMPATI_CHOSEONG_SSANGTIKEUT;
			cho2CompatiChoTable[HangulCode.CHOSEONG_RIEUL] = HangulCode.COMPATI_CHOSEONG_RIEUL;
			cho2CompatiChoTable[HangulCode.CHOSEONG_MIEUM] = HangulCode.COMPATI_CHOSEONG_MIEUM;
			cho2CompatiChoTable[HangulCode.CHOSEONG_PIEUP] = HangulCode.COMPATI_CHOSEONG_PIEUP;
			cho2CompatiChoTable[HangulCode.CHOSEONG_SSANGPIEUP] = HangulCode.COMPATI_CHOSEONG_SSANGPIEUP;
			cho2CompatiChoTable[HangulCode.CHOSEONG_SIOS] = HangulCode.COMPATI_CHOSEONG_SIOS;
			cho2CompatiChoTable[HangulCode.CHOSEONG_SSANGSIOS] = HangulCode.COMPATI_CHOSEONG_SSANGSIOS;
			cho2CompatiChoTable[HangulCode.CHOSEONG_IEUNG] = HangulCode.COMPATI_CHOSEONG_IEUNG;
			cho2CompatiChoTable[HangulCode.CHOSEONG_CIEUC] = HangulCode.COMPATI_CHOSEONG_CIEUC;
			cho2CompatiChoTable[HangulCode.CHOSEONG_SSANGCIEUC] = HangulCode.COMPATI_CHOSEONG_SSANGCIEUC;
			cho2CompatiChoTable[HangulCode.CHOSEONG_CHIEUCH] = HangulCode.COMPATI_CHOSEONG_CHIEUCH;
			cho2CompatiChoTable[HangulCode.CHOSEONG_KHIEUKH] = HangulCode.COMPATI_CHOSEONG_KHIEUKH;
			cho2CompatiChoTable[HangulCode.CHOSEONG_THIEUTH] = HangulCode.COMPATI_CHOSEONG_THIEUTH;
			cho2CompatiChoTable[HangulCode.CHOSEONG_PHIEUPH] = HangulCode.COMPATI_CHOSEONG_PHIEUPH;
			cho2CompatiChoTable[HangulCode.CHOSEONG_HIEUH] = HangulCode.COMPATI_CHOSEONG_HIEUH;
		}
		
		public static function convertCho2CompatiCho(hangulCode:uint):uint {
			var choCode:uint;
			choCode = cho2CompatiChoTable[hangulCode];
			if (choCode == 0) {
				choCode = HangulCode.COMPATI_NONE;
			}
			return choCode;
		}
		
		public static function convertCho2Jong(hangulCode:uint):uint {
			var jongCode:uint;
			jongCode = cho2JongTable[hangulCode];
			if (jongCode == 0) {
				jongCode = HangulCode.JONGSEONG_NONE;
			}
			return jongCode;
		}
		
		public static function convertJong2JC(jongCode:uint):Array {
			var jc:Array = jcTable[jongCode];
			return jc;
		}
		
		public static function combineJongSeongs(currJong:uint, insertJong:uint):uint {
			var jongCode:uint;
			jongCode = jongSeongsTable[currJong + "_" + insertJong];
			if (jongCode == 0) {
				jongCode = currJong;
			}
			return jongCode;
		}
		
		public static function combineJungSeongs(currJung:uint, insertJung:uint):uint {
			var jungCode:uint;
			jungCode = jungSeongsTable[currJung + "_" + insertJung];
			if (jungCode == 0) {
				jungCode = currJung;
			}
			return jungCode;
		}
	}

}